package com.tpo.jnotes.client.main;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Level;

public class Logging
{
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger( Logging.class
        .getName() );
    // TRACE, DEBUG, INFO, WARN, ERROR, FATAL

    static
    {
        logger.info( "Logger created. Log level = " + logger.getEffectiveLevel() );
    }

    public static void trace( String ... message )
    {
        Logging.log( Level.TRACE, message );
    }

    public static void debug( String message )
    {
        Logging.log( Level.DEBUG, message );
    }

    public static void info( String message )
    {
        Logging.log( Level.INFO, message );
    }

    public static void warn( String message )
    {
        Logging.log( Level.WARN, message );
    }

    public static void error( String message )
    {
        Logging.log( Level.ERROR, message );
    }

    public static void fatal( String message )
    {
        Logging.log( Level.FATAL, message );
    }

    public static void fatal( Throwable ex )
    {
        StringWriter stackTrace = new StringWriter();
        PrintWriter printWriter;
        printWriter = new PrintWriter( stackTrace );
        ex.printStackTrace( printWriter );
        fatal( "<EXCEPTION> " + stackTrace );
    }

    private static void log( Level level, String ... message )
    {
        StringBuilder sb = new StringBuilder( level.toString() );
        for( String s : message )
        {
            sb.append( s + " " );
        }
        logger.log( level, sb );
    }

}
